#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
	int id;			// 工号
	char name[32];	// 姓名
	int salary;		// 基本工资
	int bonus;		// 奖金
	int deduct;		// 扣款
	int finaly;		// 最终工资
}employee_t;

#define N 100

// 输入员工信息的函数
int input_employee(employee_t *p, int *n){
	printf("请输入当前员工人数:");
	scanf("%d", n);
	if(*n < 0 || *n > N){
		printf("输入的人数有误\n");
		return -1;
	}
	int i = 0;
	for(i = 0; i < *n; i++){
		printf("请输入第 %d 个员工的信息:\n", i+1);
		printf("工号:");
		scanf("%d", &(p[i].id));
		printf("姓名:");
		scanf("%s", p[i].name);
		printf("基本工资:");
		scanf("%d", &(p[i].salary));
		printf("奖金:");
		scanf("%d", &(p[i].bonus));
		printf("扣款:");
		scanf("%d", &(p[i].deduct));
	}
	return 0;
}

// 计算实发工资
int calculate_finaly_salary(employee_t *p, int num){
	int i = 0;
	for(i = 0; i < num; i++){
		p[i].finaly = p[i].salary + p[i].bonus - p[i].deduct;
	}
	return 0;
}

// 实际工资排序(降序)
int sort_finaly_salary(employee_t *p, int num){
	int i = 0;
	int j = 0;
	employee_t temp;
	for(i = 0; i < num - 1; i++){
		for(j = 0; j < num - 1 - i; j++){
			if(p[j].finaly < p[j+1].finaly){
				temp = p[j];
				p[j] = p[j+1];
				p[j+1] = temp;
			}
		}
	}
	return 0;
}

int main(int argc, const char *argv[]){
	employee_t Employee[N];
	int num = 0; // 保存员工的个数
	// 输入
	input_employee(Employee, &num);
	// 计算实发工资
	calculate_finaly_salary(Employee, num);
	// 排序
	sort_finaly_salary(Employee, num);
	// 输出实发工资最高的员工的信息
	printf("------------------------------------\n");
	printf("实发工资最高的员工的信息为:");
	printf("工号 : %d\n", Employee[0].id);
	printf("姓名 : %s\n", Employee[0].name);
	printf("基本工资 : %d\n", Employee[0].salary);
	printf("奖金 : %d\n", Employee[0].bonus);
	printf("扣款 : %d\n", Employee[0].deduct);
	printf("实发工资 : %d\n", Employee[0].finaly);
	printf("------------------------------------\n");
	
	return 0;
}
